<template>
  <basic-container>
    <avue-crud
      ref="crud"
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      v-model="form"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="onLoad(page, query)"
      @on-load="onLoad"
    >
      <template #menuLeft>
        <el-button
          :disabled="loading"
          size="mini"
          type="success"
          icon="el-icon-check"
          @click="handleCompleteTask(null, true)"
          plain
          >通过</el-button
        >
        <el-button
          :disabled="loading"
          size="mini"
          type="danger"
          icon="el-icon-close"
          @click="handleCompleteTask(null, false)"
          plain
          >驳回</el-button
        >
        <el-dropdown trigger="click" @command="moreOperation">
          <el-button type="primary" size="mini">
            更多操作<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item icon="el-icon-user" command="assignee"
              >变更审核人</el-dropdown-item
            >
            <el-dropdown-item icon="el-icon-user" command="transfer"
              >转办</el-dropdown-item
            >
            <el-dropdown-item icon="el-icon-user" command="delegate"
              >委托</el-dropdown-item
            >
            <el-dropdown-item icon="el-icon-s-promotion" command="copy"
              >抄送</el-dropdown-item
            >
            <el-dropdown-item icon="el-icon-warning" command="urge"
              >催办</el-dropdown-item
            >
            <el-dropdown-item icon="el-icon-s-opportunity" command="terminate"
              >终止</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
        <!-- <el-button
          :disabled="loading"
          size="mini"
          type="primary"
          icon="el-icon-user"
          @click="handleUserSelect({ type: 'assignee', checkType: 'radio' })"
          >变更审核人</el-button
        >
        <el-button
          :disabled="loading"
          size="mini"
          type="primary"
          icon="el-icon-user"
          @click="handleUserSelect({ type: 'transfer', checkType: 'radio' })"
          >转办</el-button
        >
        <el-button
          :disabled="loading"
          size="mini"
          type="primary"
          icon="el-icon-user"
          @click="handleUserSelect({ type: 'delegate', checkType: 'radio' })"
          >委托</el-button
        >
        <el-button
          :disabled="loading"
          size="mini"
          type="primary"
          icon="el-icon-s-promotion"
          @click="handleUserSelect({ type: 'copy', checkType: 'checkbox' })"
          >抄送</el-button
        >
        <el-button
          :disabled="loading"
          size="mini"
          type="primary"
          icon="el-icon-warning"
          @click="handleUrgeTask(null)"
          >催办</el-button
        >
        <el-button
          :disabled="loading"
          size="mini"
          type="primary"
          icon="el-icon-s-opportunity"
          @click="handleTerminateProcess(null)"
          >终止</el-button
        > -->
      </template>
      <template #processDefinitionName="{ row }">
        <el-link
          style="font-size: 12px"
          type="primary"
          @click="handleDetail(row)"
          >{{ row.processDefinitionName }}</el-link
        >
        <!-- <span>{{row.processDefinitionName}}</span> -->
      </template>
      <template>
        <el-tag size="mini" type="danger">挂起</el-tag>
        <el-tag size="mini">激活</el-tag>
      </template>
      <template #menu="{ row }">
        <el-button
          type="text"
          size="small"
          icon="el-icon-search"
          @click="handleFlow(row)"
          >流程图</el-button
        >
        <el-dropdown style="margin-left: 5px">
          <el-button size="mini" type="text"
            >更多操作<i class="el-icon-arrow-down el-icon--right"></i
          ></el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item
                @click.native="handleCompleteTask(row.taskId, true)"
                >通过</el-dropdown-item
              >
              <el-dropdown-item
                @click.native="handleCompleteTask(row.taskId, false)"
                >驳回</el-dropdown-item
              >
              <el-dropdown-item
                @click.native="
                  handleUserSelect(
                    { type: 'transfer', checkType: 'radio' },
                    row
                  )
                "
                >转办</el-dropdown-item
              >
              <el-dropdown-item
                @click.native="
                  handleUserSelect(
                    { type: 'delegate', checkType: 'radio' },
                    row
                  )
                "
                >委托</el-dropdown-item
              >
              <el-dropdown-item
                @click.native="
                  getProcessNodes(row.taskId, row.processInstanceId, 'dispatch')
                "
                >调度</el-dropdown-item
              >
              <el-dropdown-item
                @click.native="
                  getProcessNodes(row.taskId, row.processInstanceId, 'rollback')
                "
                >指定回退</el-dropdown-item
              >
              <el-dropdown-item
                @click.native="handleTerminateProcess(row.taskId)"
                >终止</el-dropdown-item
              >
              <el-dropdown-item
                @click.native="
                  handleUserSelect(
                    { type: 'addMultiInstance', checkType: 'checkbox' },
                    row
                  )
                "
                >加签</el-dropdown-item
              >
              <el-dropdown-item @click.native="handleDeleteMultiInstance(row)"
                >减签</el-dropdown-item
              >
              <el-dropdown-item
                @click.native="
                  handleUserSelect({ type: 'copy', checkType: 'checkbox' }, row)
                "
                >抄送</el-dropdown-item
              >
              <el-dropdown-item @click.native="handleUrgeTask(row.taskId)"
                >催办</el-dropdown-item
              >
              <el-dropdown-item @click.native="handleChangeStatus(row, false)"
                >激活</el-dropdown-item
              >
              <el-dropdown-item @click.native="handleChangeStatus(row, true)"
                >挂起</el-dropdown-item
              >
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </template>
    </avue-crud>

    <el-dialog
      :visible.sync="bpmnVisible"
      append-to-body
      :close-on-click-modal="false"
      destroy-on-close
      title="流程图"
    >
      <wf-design
        ref="bpmn"
        style="height: 500px"
        :options="bpmnOption"
      ></wf-design>
    </el-dialog>
    <!-- 人员选择弹窗 -->
    <user-select
      ref="user-select"
      :check-type="checkType"
      @onConfirm="handleUserSelectConfirm"
    ></user-select>
    <!-- 选择节点弹窗 -->
    <el-dialog
      :visible.sync="nodeVisible"
      append-to-body
      :close-on-click-modal="false"
      title="选择节点"
    >
      <avue-form
        v-if="nodeVisible"
        v-model="nodeForm"
        :option="nodeOption"
        @submit="handleNodeSubmit"
      ></avue-form>
    </el-dialog>
    <el-drawer
      :visible.sync="detailVisible"
      :title="form.processDefinitionName"
      custom-class="wf-drawer"
      size="60%"
      append-to-body
    >
      <task-detail
        v-if="detailVisible"
        :taskId="form.taskId"
        :processInstanceId="form.processInstanceId"
      ></task-detail>
    </el-drawer>
  </basic-container>
</template>

<script>
import { detail } from "@/api/plugin/workflow/process";
import {
  getList,
  completeTask,
  changeTaskStatus,
  changeTaskAssignee,
  transferTask,
  delegateTask,
  copyTask,
  urgeTask,
  terminateProcess,
  processNodes,
  rollbackTask,
  dispatchTask,
  addMultiInstance,
  deleteMultiInstance,
} from "@/api/plugin/workflow/ops";
import { userList } from "@/api/plugin/workflow/process";
import { mapGetters } from "vuex";

import UserSelect from "../process/components/user-select.vue";
import TaskDetail from "./detail.vue";
import exForm from "../mixins/ex-form";

export default {
  mixins: [exForm],
  components: {
    UserSelect,
    TaskDetail,
  },
  data() {
    return {
      checkType: "radio",
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        size: "mini",
        height: "auto",
        calcHeight: 30,
        tip: false,
        border: true,
        selection: true,
        dialogType: "drawer",
        addBtn: false,
        editBtn: false,
        delBtn: false,
        align: "center",
        searchMenuSpan: 6,
        searchSize: "mini",
        searchIndex: 3,
        searchIcon: true,
        // 刷新按钮
        refreshBtn: false,
        // 搜索按钮
        searchShowBtn: false,
        // 列显隐按钮
        columnBtn: false,
        // 去掉边框
        border: false,
        column: [
          {
            label: "流程名称",
            prop: "processDefinitionName",
            search: true,
            overHidden: true,
          },
          {
            label: "流程标识",
            prop: "processDefinitionKey",
            search: true,
            overHidden: true,
          },
          {
            label: "流水号",
            prop: "serialNumber",
            search: true,
            overHidden: true,
          },
          {
            label: "流程分类",
            row: true,
            type: "tree",
            dicUrl: "/workflow/design/category/tree",
            props: {
              label: "name",
              value: "id",
            },
            prop: "category",
            search: true,
          },
          {
            label: "当前节点",
            prop: "taskName",
            search: true,
          },
          {
            label: "审核人",
            prop: "assignee",
            search: true,
            overHidden: true,
          },
          {
            label: "申请人",
            prop: "startUsername",
            search: true,
          },
          {
            label: "创建时间",
            prop: "createTime",
            type: "datetime",
            format: "yyyy-MM-dd HH:mm",
            width: 165,
          },
          {
            label: "状态",
            prop: "isSuspended",
            type: "select",
            dicData: [
              {
                label: "挂起",
                value: true,
              },
              {
                label: "激活",
                value: false,
              },
            ],
            search: true,
          },
          {
            label: "时间范围",
            prop: "startTimeRange",
            type: "datetime",
            dataType: "string",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            hide: true,
            search: true,
            searchRange: true,
          },
        ],
      },
      data: [],
      bpmnVisible: false,
      bpmnOption: {},
      nodeVisible: false,
      nodeForm: {},
      nodeOption: {
        column: [
          {
            label: "节点",
            prop: "nodeId",
            type: "select",
            props: {
              label: "nodeName",
              value: "nodeId",
            },
            span: 24,
            rules: [
              { required: true, message: "请选择节点", trigger: "change" },
            ],
          },
        ],
      },
      nodeType: "",
      detailVisible: false,
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.taskId);
      });
      return ids.join(",");
    },
  },
  methods: {
    // 更多操作
    moreOperation(command) {
      console.log("command跟多操作", command);
      switch (command) {
        case "assignee":
          this.handleUserSelect({ type: "assignee", checkType: "radio" });
          break;
        case "transfer":
          this.handleUserSelect({ type: "transfer", checkType: "radio" });
          break;
        case "delegate":
          handleUserSelect({ type: "delegate", checkType: "radio" });
          break;
        case "copy":
          handleUserSelect({ type: "copy", checkType: "checkbox" });
          break;
        case "urge":
          handleUrgeTask(null);
          break;
        case "terminate":
          handleTerminateProcess(null);
          break;
        default:
          break;
      }
    },
    handleDeleteMultiInstance(row) {
      const { taskId } = row;
      this.$confirm(`确定要将选中的任务减签吗？`, "警告", {
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          deleteMultiInstance({ taskId })
            .then(() => {
              this.$message.success("减签成功");
              this.onLoad(this.page, this.query);
            })
            .catch(() => {
              this.loading = false;
            });
        })
        .catch(() => {});
    },
    handleDetail(row) {
      this.dynamicRoute(row, "detail", true).then(() => {
        this.form = { ...row };
        this.detailVisible = true;
      });
    },
    rollbackTask,
    dispatchTask,
    handleNodeSubmit(form, done) {
      const { nodeId, taskId } = form;
      const param = { nodeId, taskId };
      this.loading = true;
      this[`${this.nodeType}Task`](param)
        .then(() => {
          this.$message.success("操作成功");
          this.form = {};
          done();
          this.nodeVisible = false;
          this.onLoad(this.page, this.query);
        })
        .catch(() => {
          done();
          this.loading = false;
        });
    },
    getProcessNodes(taskId, processInstanceId, type) {
      this.nodeType = type;
      const param = { processInstanceId };
      if (type == "rollback") param.taskId = taskId;
      processNodes(param).then((res) => {
        this.findObject(this.nodeOption.column, "nodeId").dicData = res.data;
        this.nodeVisible = true;
        this.nodeForm = {
          ...this.nodeForm,
          taskId,
          processInstanceId,
        };
      });
    },
    handleCompleteTask(taskId, pass) {
      if (!taskId) {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        taskId = this.ids;
      }
      this.$confirm(
        `确定要将选中的任务全部<span style='color: red;'> ${
          pass ? "通过" : "驳回"
        } </span>吗？`,
        "警告",
        {
          type: "warning",
          dangerouslyUseHTMLString: true,
        }
      )
        .then(() => {
          this.loading = true;
          completeTask({ taskId, pass })
            .then(() => {
              this.$message.success("操作成功");
              this.onLoad(this.page, this.query);
            })
            .catch(() => {
              this.loading = false;
            });
        })
        .catch(() => {});
    },
    handleUrgeTask(taskId) {
      if (!taskId) {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        taskId = this.ids;
      }
      this.$confirm(
        `确定要将催办选中的任务吗？若任务没有审核人此操作无效`,
        "警告",
        {
          type: "warning",
        }
      )
        .then(() => {
          this.loading = true;
          urgeTask({ taskId })
            .then(() => {
              this.$message.success("催办成功");
              this.onLoad(this.page, this.query);
            })
            .catch(() => {
              this.loading = false;
            });
        })
        .catch(() => {});
    },
    handleTerminateProcess(taskId) {
      if (!taskId) {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        taskId = this.ids;
      }
      this.$confirm(`确定要将选中的任务流程终止吗？`, "警告", {
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          terminateProcess({ taskId })
            .then(() => {
              this.$message.success("终止成功");
              this.onLoad(this.page, this.query);
            })
            .catch(() => {
              this.loading = false;
            });
        })
        .catch(() => {});
    },
    handleChangeStatus(row, isSuspended) {
      this.loading = true;
      changeTaskStatus({ taskId: row.taskId, isSuspended })
        .then(() => {
          this.$message.success("操作成功");
          this.onLoad(this.page, this.query);
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 人员选择弹窗
    handleUserSelect({ type, checkType }, row) {
      if (row) this.selectionList = [row];
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$refs["user-select"].visible = true;
      this.userSelectType = type;
      this.checkType = checkType;
    },
    handleUserSelectConfirm(id, name) {
      switch (this.userSelectType) {
        case "assignee":
          this.$confirm(
            `确定要将选中的任务审核人变更为<span style='color: red;'> ${name} </span>吗？`,
            "提示",
            {
              type: "warning",
              dangerouslyUseHTMLString: true,
            }
          )
            .then(() => {
              this.$refs["user-select"].visible = false;
              this.loading = true;
              changeTaskAssignee({ taskId: this.ids, assignee: id })
                .then(() => {
                  this.$message.success("操作成功");
                  this.onLoad(this.page, this.query);
                  this.selectionList = [];
                })
                .catch(() => {
                  this.loading = false;
                });
            })
            .catch(() => {});
          break;
        case "transfer":
          this.$confirm(
            `确定要将选中的任务转办给<span style='color: red;'> ${name} </span>吗？`,
            "提示",
            {
              type: "warning",
              dangerouslyUseHTMLString: true,
            }
          )
            .then(() => {
              this.$refs["user-select"].visible = false;
              this.loading = true;
              transferTask({ taskId: this.ids, assignee: id })
                .then(() => {
                  this.$message.success("转办成功");
                  this.onLoad(this.page, this.query);
                  this.selectionList = [];
                })
                .catch(() => {
                  this.loading = false;
                });
            })
            .catch(() => {});
          break;
        case "delegate":
          this.$confirm(
            `确定要将选中的任务委托给<span style='color: red;'> ${name} </span>吗？`,
            "提示",
            {
              type: "warning",
              dangerouslyUseHTMLString: true,
            }
          )
            .then(() => {
              this.$refs["user-select"].visible = false;
              this.loading = true;
              delegateTask({ taskId: this.ids, assignee: id })
                .then(() => {
                  this.$message.success("委托成功");
                  this.onLoad(this.page, this.query);
                  this.selectionList = [];
                })
                .catch(() => {
                  this.loading = false;
                });
            })
            .catch(() => {});
          break;
        case "copy":
          this.$confirm(
            `确定要将选中的任务抄送给<span style='color: red;'> ${name} </span>吗？`,
            "提示",
            {
              type: "warning",
              dangerouslyUseHTMLString: true,
            }
          )
            .then(() => {
              this.$refs["user-select"].visible = false;
              copyTask({ taskId: this.ids, assignee: id })
                .then(() => {
                  this.$refs.crud.toggleSelection();
                  this.$message.success("抄送成功");
                })
                .catch(() => {
                  this.loading = false;
                });
            })
            .catch(() => {});
          break;
        case "addMultiInstance":
          this.$confirm(`确定要将选中的人员加签吗？`, "提示", {
            type: "warning",
          })
            .then(() => {
              this.$refs["user-select"].visible = false;
              addMultiInstance({ taskId: this.ids, assignee: id })
                .then(() => {
                  this.$message.success("加签成功");
                  this.onLoad(this.page, this.query);
                  this.selectionList = [];
                })
                .catch(() => {
                  this.loading = false;
                });
            })
            .catch(() => {});
          break;
      }
    },
    handleFlow(row) {
      const { taskId, processInstanceId } = row;
      detail({ taskId, processInsId: processInstanceId }).then((res) => {
        const { process, flow } = res.data;

        this.bpmnOption = {
          mode: "view",
          xml: process.xml,
          flows: this.handleResolveFlows(flow),
        };

        this.bpmnVisible = true;
      });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    async onLoad(page, params = {}) {
      this.loading = true;
      const param = Object.assign(params, this.query);
      const { assignee } = param;
      if (assignee) {
        const res = await userList(1, -1, { name: assignee });
        if (res.data.records && res.data.records.length > 0) {
          param.assignee = res.data.records.map((d) => d.id).join(",");
        } else {
          this.page.total = 0;
          this.data = [];
          this.loading = false;
        }
      }
      getList(page.currentPage, page.pageSize, param)
        .then((res) => {
          const data = res.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="scss">
.wf-drawer {
  .el-drawer__body {
    padding: 0 20px;
    overflow: auto;
  }
}
</style>
